package com.yoga.manager.admin.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.PlainVipBO;
import com.yoga.common.bo.PrivateVipBO;
import com.yoga.common.bo.VipCareBO;
import com.yoga.common.bo.YogaUserBO;
import com.yoga.common.page.PageBO;
import com.yoga.common.qto.PlainVipQTO;
import com.yoga.common.qto.PrivateVipQTO;
import com.yoga.common.qto.YogaUserQTO;
import com.yoga.dao.dao.PlainVipDao;
import com.yoga.dao.dao.PrivateVipDao;
import com.yoga.dao.dao.YogaUserDao;
import com.yoga.dao.domain.PlainVipDO;
import com.yoga.dao.domain.PrivateVipDO;
import com.yoga.dao.domain.YogaUserDO;
import com.yoga.manager.admin.AdminYogaUserManager;
import com.yoga.manager.app.AppPlainVipManager;
import com.yoga.manager.app.AppPrivateVipManager;
import com.yoga.manager.app.AppYogaUserManager;
import com.yoga.manager.beanUtil.PlainVipBeanUtil;
import com.yoga.manager.beanUtil.PrivateVipBeanUtil;
import com.yoga.manager.beanUtil.YogaUserBeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by hdx on 2020/9/29.
 */
@Component
public class AdminYogaUserManagerImpl implements AdminYogaUserManager {
    Logger logger = LoggerFactory.getLogger(AdminYogaUserManagerImpl.class);

    @Resource
    private YogaUserDao yogaUserDao;

    @Resource
    private PlainVipDao plainVipDao;

    @Resource
    private PrivateVipDao privateVipDao;

    @Resource
    private AppPlainVipManager plainVipManager;

    @Resource
    private AppPrivateVipManager privateVipManager;

    @Override
    public Boolean updateData(YogaUserBO yogaUserBO) throws AppException {
        if (yogaUserBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (yogaUserBO.getYogaUserId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        int num = yogaUserDao.updateById(YogaUserBeanUtil.yogaUserBOToDO(yogaUserBO));
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }

    @Override
    public YogaUserBO findById(Long yogaUserId) throws AppException {
        if (yogaUserId == null) {
            throw new AppException(AppErrorCodeEnum.YOGA_USER_ID_NULL);
        }
        YogaUserDO yogaUserDO = yogaUserDao.findById(yogaUserId);
        if (yogaUserDO == null) {
            throw new AppException(AppErrorCodeEnum.YOGA_USER_NULL);
        }
        YogaUserBO yogaUserBO = YogaUserBeanUtil.yogaUserDOToBO(yogaUserDO);
        PlainVipBO plainVipBO = plainVipManager.getPlainVipByUserId(yogaUserId);
        PrivateVipBO privateVipBO = privateVipManager.getPrivateVipByUserId(yogaUserId);
        yogaUserBO.setPlainVipBO(plainVipBO);
        yogaUserBO.setPrivateVipBO(privateVipBO);

        return yogaUserBO;
    }

    @Override
    public PageBO<YogaUserBO> findForPage(YogaUserQTO yogaUserQTO) throws AppException {
        if (yogaUserQTO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        PageBO<YogaUserBO> pageBO = new PageBO<>();
        pageBO.setQuery(yogaUserQTO);
        Integer count = yogaUserDao.findForPageCount(yogaUserQTO);
        if (count == null || count <= 0) {
            return pageBO;
        }
        List<YogaUserBO> boList = null;
        if (yogaUserQTO.getVipStatus() == 0) {//全部
            boList = findAllUser(yogaUserQTO);
        } else if (yogaUserQTO.getVipStatus() == 1){//团课会员
            boList = findPlainVipUser(yogaUserQTO);
        } else if (yogaUserQTO.getVipStatus() == 2) {//私教会员

        }


        pageBO.setTotalCount(count);
        pageBO.setData(boList);

        return pageBO;
    }

    /**
     * 查询全部会员
     * @param yogaUserQTO
     * @return
     */
    private List<YogaUserBO> findAllUser(YogaUserQTO yogaUserQTO) {
        List<YogaUserBO> boList = YogaUserBeanUtil.doList2BOList(yogaUserDao.findForPage(yogaUserQTO));
        if (!CollectionUtils.isEmpty(boList)) {
            List<Long> yogaUserIds = boList.stream().map(YogaUserBO::getYogaUserId).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(yogaUserIds)) {
                PlainVipQTO plainVipQTO = new PlainVipQTO();
                plainVipQTO.setYogaUserIds(yogaUserIds);
                plainVipQTO.setNeedPaging(false);
                List<PlainVipBO> plainVipBOs = PlainVipBeanUtil.doList2BOList(plainVipDao.findForPage(plainVipQTO));
                PrivateVipQTO privateVipQTO = new PrivateVipQTO();
                privateVipQTO.setYogaUserIds(yogaUserIds);
                privateVipQTO.setNeedPaging(false);
                List<PrivateVipBO> privateVipBOs = PrivateVipBeanUtil.doList2BOList(privateVipDao.findForPage(privateVipQTO));
                if (!CollectionUtils.isEmpty(plainVipBOs)) {
                    Map<Long, PlainVipBO> plainVipBOMap = plainVipBOs.stream().collect(Collectors.toMap(PlainVipBO::getYogaUserId, Function.identity()));
                    boList.stream().filter(yogaUserBO -> plainVipBOMap.containsKey(yogaUserBO.getYogaUserId()))
                            .forEach(yogaUserBO -> yogaUserBO.setPlainVipBO(plainVipBOMap.get(yogaUserBO.getYogaUserId())));
                }
                if (!CollectionUtils.isEmpty(privateVipBOs)) {
                    Map<Long, PrivateVipBO> privateVipBOMap = privateVipBOs.stream().collect(Collectors.toMap(PrivateVipBO::getYogaUserId, Function.identity()));
                    boList.stream().filter(yogaUserBO -> privateVipBOMap.containsKey(yogaUserBO.getYogaUserId()))
                            .forEach(yogaUserBO -> yogaUserBO.setPrivateVipBO(privateVipBOMap.get(yogaUserBO.getYogaUserId())));
                }
            }
        }
        return boList;
    }

    /**
     * 团课会员信息
     * @param yogaUserQTO
     * @return
     */
    private List<YogaUserBO> findPlainVipUser(YogaUserQTO yogaUserQTO) {
        PlainVipQTO plainVipQTO = new PlainVipQTO();
        plainVipQTO.setVipType(1);//查询团课会员和次卡会员
        plainVipQTO.setNeedPaging(yogaUserQTO.isNeedPaging());
        plainVipQTO.setPageSize(yogaUserQTO.getPageSize());
        plainVipQTO.setCurrentPage(yogaUserQTO.getCurrentPage());

        List<YogaUserBO> yogaUserBOs = new ArrayList<>();
        List<PlainVipDO> plainVipDOs = plainVipDao.findForPage(plainVipQTO);
        if (!CollectionUtils.isEmpty(plainVipDOs)) {
            List<Long> yogaUserIds = plainVipDOs.stream().map(PlainVipDO::getYogaUserId).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(yogaUserIds)) {
                yogaUserQTO.setYogaUserIds(yogaUserIds);
                yogaUserBOs = findAllUser(yogaUserQTO);
            }
        }

        return yogaUserBOs;
    }

    private List<YogaUserBO> findPrivateVipUser(YogaUserQTO yogaUserQTO) {
        PrivateVipQTO privateVipQTO = new PrivateVipQTO();
        privateVipQTO.setNeedPaging(yogaUserQTO.isNeedPaging());
        privateVipQTO.setPageSize(yogaUserQTO.getPageSize());
        privateVipQTO.setCurrentPage(yogaUserQTO.getCurrentPage());
        List<YogaUserBO> yogaUserBOs = new ArrayList<>();
        privateVipQTO.setVipType(1);//已开通的私教会员
        List<PrivateVipDO> privateVipDOs = privateVipDao.findForPage(privateVipQTO);
        if (CollectionUtils.isEmpty(privateVipDOs)) {
            List<Long> yogaUserIds = privateVipDOs.stream().map(PrivateVipDO::getYogaUserId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(yogaUserIds)) {
                yogaUserQTO.setYogaUserIds(yogaUserIds);
                yogaUserBOs = findAllUser(yogaUserQTO);
            }
        }

        return yogaUserBOs;
    }

    @Override
    public VipCareBO findVipCardByUserId(Long yogaUserId) throws AppException {
        if (yogaUserId == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        PlainVipBO plainVipBO = plainVipManager.getPlainVipByUserId(yogaUserId);
        PrivateVipBO privateVipBO = privateVipManager.getPrivateVipByUserId(yogaUserId);
        VipCareBO vipCareBO = new VipCareBO(yogaUserId);
        vipCareBO.setPlainVipBO(plainVipBO);
        vipCareBO.setPrivateVipBO(privateVipBO);

        return vipCareBO;
    }

}
